package com.pur.zsms.pmsnmanage.role.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.documentsmanage.po.DomPo;
import com.pur.zsms.pmsnmanage.menu.dao.IMenuDao;
import com.pur.zsms.pmsnmanage.menu.po.MenuPo;
import com.pur.zsms.pmsnmanage.role.dao.IRoleDao;
import com.pur.zsms.pmsnmanage.role.po.RoleMenuPo;
import com.pur.zsms.pmsnmanage.role.po.RolePo;
import com.pur.zsms.pmsnmanage.role.service.IRoleService;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.dao.IUserDao;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.systemsetting.datadictionary.po.CodeType;
import com.pur.zsms.systemsetting.datadictionary.vo.CodeVo;

@Service("roleService")
public class RoleServiceImpl implements IRoleService {

	@Resource(name = "roleDao")
	private IRoleDao roleDaoImpl;

	@Resource(name = "menuDao")
	private IMenuDao menuDaoImpl;

	public void setRoleDaoImpl(IRoleDao roleDaoImpl) {
		this.roleDaoImpl = roleDaoImpl;
	}

	public void setMenuDaoImpl(IMenuDao menuDaoImpl) {
		this.menuDaoImpl = menuDaoImpl;
	}

	public PaginationSupport listRole(PageController pageController, RoleVo roleVo) throws GenericException {
		return roleDaoImpl.listRole(pageController, roleVo);

	}

	public void addRole(RoleVo c) throws GenericException {
		RolePo po = new RolePo();
		try {
			PropertyUtils.copyProperties(po, c);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("输入参数有误!");
		}
		po.setInsertTime(PubFun.getCurrentDate() + " " + PubFun.getCurrentTime());
		roleDaoImpl.addRole(po);
	}

	public void updateRole(RoleVo c) throws GenericException {
		RolePo po = new RolePo();
		try {
			PropertyUtils.copyProperties(po, c);
		} catch (Exception e) {
			throw new RuntimeException("查询失败!");
		}
		po.setUpdateTime(PubFun.getCurrentDate() + " " + PubFun.getCurrentTime());
		roleDaoImpl.addRole(po);
	}

	/*
	 * public void deleteRole(Long roleId) throws GenericException {
	 * List<UserRolePo> list = roleDaoImpl.listUserRoleByRoleId(roleId); if(list
	 * != null && list.size() > 0){ throw new
	 * RuntimeException("该角色已经被用户引用，无法进行删除!"); } roleDaoImpl.deleteRole(roleId);
	 * }
	 */
	public List<MenuPo> listAllMenu() throws GenericException {
		return menuDaoImpl.findMenus();
	}

	@Override
	public void addRoleMenu(RoleVo roleVo, Long[] menuIds) throws GenericException {
		roleDaoImpl.addRoleMenu(roleVo, menuIds);
	}

	public void addRoleMenu(RoleVo roleVo) throws GenericException {
		roleDaoImpl.addRoleMenu(roleVo);
	}

	@Override
	public RolePo findRole(Long roleId) throws GenericException {
		return roleDaoImpl.findRoleById(roleId);
	}

	public RolePo findRoleByCompanyIdForSupplier(int companyId) throws GenericException {
		return roleDaoImpl.findRoleByCompanyIdForSupplier(companyId);
	}

	@Override
	public List<RoleMenuPo> findMenusByRole(Long roleId) throws GenericException {
		return roleDaoImpl.findMenusByRole(roleId);
	}

	@Override
	public void updateRoleMenu(RoleVo roleVo) throws GenericException {
		roleDaoImpl.updateRoleMenu(roleVo);
	}

	@Override
	public void deleteRoleMenu(Long roleId) throws GenericException {
		List<RoleMenuPo> list = roleDaoImpl.findMenusByRole(roleId);
		for (int i = 0; i < list.size(); i++) {
			roleDaoImpl.deleteRoleMenu(list.get(i));
		}
		roleDaoImpl.deleteRole(roleId);
	}

	@Override
	public List<UserRolePo> findUserRoles(Long roleId) throws GenericException {
		return roleDaoImpl.findUserRoles(roleId);
	}

	public List<RolePo> getRolePoByRoleNameUserId(UserPo userPo, String roleName) throws GenericException {
		return roleDaoImpl.getRolePoByRoleNameUserId(userPo, roleName);
	}

	public String getAllMenuJson(int id) throws GenericException {
		StringBuffer jsonString = new StringBuffer("");
		List<MenuPo> menuPolist = roleDaoImpl.getAllMenuPo(id);
		if (menuPolist != null && menuPolist.size() > 0) {
			jsonString.append("[");
			for (MenuPo menuPo : menuPolist) {
				jsonString.append("{\"");
				jsonString.append("id");
				jsonString.append("\":\"");
				jsonString.append(menuPo.getMenuId());
				jsonString.append("\",\"text\":\"");
				jsonString.append(menuPo.getMenuName());
				jsonString.append("\"");

				if (menuPo.getMenuLink() == null || menuPo.getMenuLink().equals("")) {
					jsonString.append(",\"state\":\"closed\"");
					jsonString.append(",\"folder\":\"1\"");
				}
				/*
				 * if (menuPo.getDomLink() != null &&
				 * !menuPo.getDomLink().equals("")) {
				 * jsonString.append(",\"link\":\""); jsonString.append("1");
				 * jsonString.append("\""); }
				 */
				jsonString.append("},");
			}
			jsonString = new StringBuffer(jsonString.substring(0, jsonString.length() - 1));
			jsonString.append("]");
			return jsonString.toString();
		} else {
			return "[]";
		}
	}

	public String getAllMenuJson(String menuIdAll) throws GenericException {

		// 处理要选中的页面
		String[] menuIdArray = menuIdAll.split(",");

		StringBuffer jsonString = new StringBuffer("");
		// 先取得2级菜单
		List<MenuPo> menuPolist = roleDaoImpl.getAllMenuPo(0);
		if (menuPolist != null && menuPolist.size() > 0) {
			jsonString.append("[{ \"id\":0, \"text\":\"菜单目录\" ,\"state\":\"open\",\"folder\":\"1\",\"children\":[");
			for (MenuPo menuPo : menuPolist) {
				jsonString.append("{\"");
				jsonString.append("id");
				jsonString.append("\":\"");
				jsonString.append(menuPo.getMenuId());
				jsonString.append("\",\"text\":\"");
				jsonString.append(menuPo.getMenuName());
				jsonString.append("\"");

				if (menuPo.getMenuLink() == null || menuPo.getMenuLink().equals("")) {
					jsonString.append(",\"state\":\"open\"");
					jsonString.append(",\"folder\":\"1\"");
				}

				for (String menuId : menuIdArray) {
					if (menuPo.getMenuId() == Long.valueOf(menuId) && menuPo.getMenuLink() != null && !menuPo.getMenuLink().equals("")) {
						jsonString.append(",\"checked\":true");
					}
				}

				String children = getChildrenMenuJson(menuPo.getMenuId(), menuIdArray);
				if (!children.equals("")) {
					jsonString.append(children);
				}

				jsonString.append("},");
			}
			jsonString = new StringBuffer(jsonString.substring(0, jsonString.length() - 1));
			jsonString.append("]}]");
			return jsonString.toString();
		} else {
			return "[]";
		}
	}

	public String getChildrenMenuJson(long id, String[] menuIdArray) throws GenericException {
		StringBuffer jsonString = new StringBuffer("");
		List<MenuPo> menuPolist = roleDaoImpl.getAllMenuPo(id);
		if (menuPolist != null && menuPolist.size() > 0) {
			jsonString.append(",\"children\":[");
			for (MenuPo menuPo : menuPolist) {
				jsonString.append("{\"");
				jsonString.append("id");
				jsonString.append("\":\"");
				jsonString.append(menuPo.getMenuId());
				jsonString.append("\",\"text\":\"");
				jsonString.append(menuPo.getMenuName());
				jsonString.append("\"");

				if (menuPo.getMenuLink() == null || menuPo.getMenuLink().equals("")) {
					jsonString.append(",\"state\":\"open\"");
					jsonString.append(",\"folder\":\"1\"");
				}

				for (String menuId : menuIdArray) {
					if (menuPo.getMenuId() == Long.valueOf(menuId) && menuPo.getMenuLink() != null && !menuPo.getMenuLink().equals("")) {
						jsonString.append(",\"checked\":true");
					}
				}

				String children = getChildrenMenuJson(menuPo.getMenuId(), menuIdArray);
				if (!children.equals("")) {
					jsonString.append(children);
				}
				jsonString.append("},");
			}
			jsonString = new StringBuffer(jsonString.substring(0, jsonString.length() - 1));
			jsonString.append("]");
			return jsonString.toString();
		} else {
			return "";
		}
	}

}
